package com.freely.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.freely.dao.ChatGroupDao;
import com.freely.domain.Group;
import com.freely.service.ChatGroupService;

@Transactional
public class ChatGroupServiceImpl implements ChatGroupService {
	private ChatGroupDao chatGroupDao;

	public void setChatGroupDao(ChatGroupDao chatGroupDao) {
		this.chatGroupDao = chatGroupDao;
	}

	@Override
	public List<Group> findByCondition(DetachedCriteria criteria) {
		return chatGroupDao.findByCondition(criteria);
	}

	@Override
	public void addChatGroup(Group group) {
		chatGroupDao.addChatGroup(group);
	}

	/**
	 * 更新群成员，flag为true为添加成员，false为删减成员
	 */
	@Override
	public void updateGroupMember(Group group,boolean flag) {
		chatGroupDao.updateGroup(group);
		JSONObject modifyObj = new JSONObject();
		if(flag) {
			modifyObj.put("group_sum", group.getGroup_sum() + 1);
		}else {
			modifyObj.put("group_sum", group.getGroup_sum() - 1);
		}
		update(modifyObj, null);
	}

	@Override
	public void deleteGroup(Group group) {
		chatGroupDao.deleteGroup(group);
	}

	@Override
	public int update(JSONObject modifyObj, JSONObject conditionObj) {
		return chatGroupDao.update(modifyObj,conditionObj);
	}
	
	
}
